package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.MerchantAccountQueryDO;
import com.recharge.domain.query.MerchantQueryDO;
import com.recharge.domain.vo.MerchantAccountDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.service.MerchantAccountService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/merchant-account")
public class MerchantAccountController extends BaseController {


    @Autowired
    private MerchantAccountService merchantAccountService;


    @RequiresPermissions("merchant-account:edit")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    @ApiOperation(value="编辑下游商家支付帐户信息", notes="")
    public @ResponseBody
    ObjectResponse<MerchantAccountDO> edit(@RequestBody MerchantAccountDO domain) {

        ObjectResponse<MerchantAccountDO> response = new ObjectResponse<>();
        domain.setModifier(getUserName());

        ResultDO result = merchantAccountService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("merchant-account:get")
    @RequestMapping(value="/get.html", method = RequestMethod.POST)
    @ApiOperation(value="获取下游商家支付帐户信息", notes="")
    public @ResponseBody
    ObjectResponse<MerchantAccountDO> get(@RequestBody MerchantAccountDO domain) {

        ObjectResponse<MerchantAccountDO> response = new ObjectResponse<>();

        ResultDO result = merchantAccountService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((MerchantAccountDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("merchant-account:delete")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除下游商家支付帐户信息", notes="")
    public @ResponseBody
    ObjectResponse delete(@RequestBody MerchantAccountDO domain) {

        ObjectResponse response = new ObjectResponse();
        UserLoginDO userLoginDO = getUserLoginInfo();

        ResultDO result = merchantAccountService.remove(domain.getId(), userLoginDO.getUsername());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("merchant-account:page")
    @RequestMapping(value="/page.html", method = RequestMethod.POST)
    @ApiOperation(value="分页查询下游商家支付帐户", notes="")
    public @ResponseBody  ObjectResponse<PageDO> page(@RequestBody MerchantAccountQueryDO query) {

        ObjectResponse<PageDO> response = new ObjectResponse<>();

        ResultDO result = merchantAccountService.find(query);

        if(result.isSuccess()) {
            response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }
}
